package edu.c06190115.cardiac;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class Main_Screen extends Activity {

	private String fileName;
	private int age = 0;
	private String setAge = "X";
	private String setYesNo = "X";
	private String setRate = "X";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main__screen);
		Calendar c = Calendar.getInstance();
		fileName = "" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DATE) + "-" + c.get(Calendar.YEAR)
				+ "-" + c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND) + ".txt";
		try {
			File root = new File(Environment.getExternalStorageDirectory(), "Cardiac");
			if (!root.exists()) {
				root.mkdirs();
			}
			File gpxfile = new File(root, fileName);
			FileWriter writer = new FileWriter(gpxfile);
			writer.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}

	public void myClickListener(View view){
		
		
		
		String complete;
		String rate;
		String algorithmName = "nothing";
		String rhythm;
		Integer digits;
		Button yesButton;
		Button noButton;
		Button adultButton;
		Button pedButton;
		
		Spinner rateSpinner = (Spinner)findViewById(R.id.heartRate);
		Spinner rhythmSpinner = (Spinner)findViewById(R.id.heartRythm);
		Spinner pedRhythmSpinner = (Spinner)findViewById(R.id.pedHeartRythm);
		Spinner pedRateSpinner = (Spinner)findViewById(R.id.pedHeartRate);
		
		
		yesButton = (Button)findViewById(R.id.Yes);
		noButton = (Button)findViewById(R.id.No);
		adultButton = (Button)findViewById(R.id.Adult);
		pedButton = (Button)findViewById(R.id.Pediatric);
		
		switch(view.getId()){
		case R.id.Adult:
			adultButton.setBackgroundColor(Color.DKGRAY);
			pedButton.setBackgroundColor(Color.parseColor("#218fae"));
			setAge = "1";
			age = 1;
			break;
		case R.id.Pediatric:
			pedButton.setBackgroundColor(Color.DKGRAY);
			adultButton.setBackgroundColor(Color.parseColor("#218fae"));
			setAge = "2";
			age = 0;
			break;
		case R.id.Yes:
			
			yesButton.setBackgroundColor(Color.DKGRAY);
			noButton.setBackgroundColor(Color.parseColor("#218fae"));
			
			if (age == 1){
				rateSpinner.setVisibility(View.VISIBLE);
				rhythmSpinner.setVisibility(View.INVISIBLE);
				pedRateSpinner.setVisibility(View.INVISIBLE);
				pedRhythmSpinner.setVisibility(View.INVISIBLE);
			} else {
				rateSpinner.setVisibility(View.INVISIBLE);
				rhythmSpinner.setVisibility(View.INVISIBLE);
				pedRateSpinner.setVisibility(View.VISIBLE);
				pedRhythmSpinner.setVisibility(View.INVISIBLE);
			}
			setYesNo = "1";
			break;
		case R.id.No:
			noButton.setBackgroundColor(Color.DKGRAY);
			yesButton.setBackgroundColor(Color.parseColor("#218fae"));
			
			if (age == 1){
				rateSpinner.setVisibility(View.INVISIBLE);
				rhythmSpinner.setVisibility(View.VISIBLE);
				pedRateSpinner.setVisibility(View.INVISIBLE);
				pedRhythmSpinner.setVisibility(View.INVISIBLE);
			} else {
				rateSpinner.setVisibility(View.INVISIBLE);
				rhythmSpinner.setVisibility(View.INVISIBLE);
				pedRateSpinner.setVisibility(View.INVISIBLE);
				pedRhythmSpinner.setVisibility(View.VISIBLE);
			}
			setYesNo = "0";
			break;
		case R.id.selectAlgorithm:
			if (rateSpinner.getVisibility() == View.VISIBLE){
				rate = rateSpinner.getSelectedItem().toString();
				if (rate.equals("HR less than 60")) setRate = "0";
				if (rate.equals("HR greater than 100")) setRate = "1";
				if (rate.equals("Adult Heart Rate")){
					Toast.makeText(this, "Heart Rate not set, please set Heart Rate", Toast.LENGTH_SHORT).show();
					setRate = "X";
				}
			}
			if (rhythmSpinner.getVisibility() == View.VISIBLE){
				rhythm = rhythmSpinner.getSelectedItem().toString();
				if (rhythm.equals("Asystole")) setRate = "2";
				if (rhythm.equals("PEA")) setRate = "3";
				if (rhythm.equals("V-fib/Pulseless V-tach")) setRate = "4";
				if (rhythm.equals("Adult Heart Rhythm")){
					Toast.makeText(this, "Heart Rhythm not set, please set Heart Rhythm", Toast.LENGTH_SHORT).show();
					setRate = "X";
				}
			}	
			if (pedRhythmSpinner.getVisibility() == View.VISIBLE){
				rhythm = pedRhythmSpinner.getSelectedItem().toString();
				if (rhythm.equals("Asystole and PEA")) setRate = "0";
				if (rhythm.equals("V-Fib/Pulseless V-Tach")) setRate = "1";
				if (rhythm.equals("Pediatric Heart Rhythm")){
					Toast.makeText(this, "Heart Rhythm not set, please set Heart Rhythm", Toast.LENGTH_SHORT).show();
					setRate = "X";
				}
				
				
				}
			
			if (pedRateSpinner.getVisibility() == View.VISIBLE){
				rate = pedRateSpinner.getSelectedItem().toString();
				if (rate.equals("HR less than 110"))setRate = "2";
				
					
				if (rate.equals("HR greater than 160")) setRate = "3";
				if (rate.equals("Pediatric Heart Rate")){
					Toast.makeText(this, "Heart Rate not set, please set Heart Rate", Toast.LENGTH_SHORT).show();
					setRate = "X";
				}
			}
			
			complete = setAge + setYesNo + setRate;
		if (!complete.contains("X")){
				digits = Integer.parseInt(complete);
				switch (digits){
				//adultbradycardia
				case 110:
					algorithmName = "Bradycardia with Pulse, Adult";
					Toast.makeText(this, "Bradycardia with Pulse, Adult", Toast.LENGTH_SHORT).show();
					break;
				//Adult v-tach algorithm
				case 111:
					algorithmName = "V-Tach with Pulse, Adult";
					Toast.makeText(this, "V-Tach with Pulse, Adult", Toast.LENGTH_SHORT).show();
					break;
				//Adult Asystole algorithm
				case 102:
					algorithmName = "Asystole, Adult";
					Toast.makeText(this, "Asystole, Adult", Toast.LENGTH_SHORT).show();
					break;
				//PEA
				case 103:
					algorithmName = "PEA, Adult";
					Toast.makeText(this, "PEA, Adult", Toast.LENGTH_SHORT).show();
					break;
				//V-fib/pulseless vtach
				case 104:
					algorithmName = "V-Fib, Adult";
					Toast.makeText(this, "V-Fib, Adult", Toast.LENGTH_SHORT).show();
					break;
				
				//pediatric Asytole and PEA
				case 200:
					algorithmName = "Asystole, Pediatric";
					Toast.makeText(this, "Asystole, Pediatric", Toast.LENGTH_SHORT).show();
					break;
					//vfib and vtach
				case 201:
					algorithmName = "V-fib and Pulseless V-tach, Pediatric";
					Toast.makeText(this, "V-fib and Pulseless V-tach, Pediatric", Toast.LENGTH_SHORT).show();
					break;
					//pediatric bradycardia
	///-------------------------------------
				//THESES TWO CASES DO NOT WORK 
				case 212:
					algorithmName = "Bradycardia with Pulse, Pediatric";
					Toast.makeText(this, "Bradycardia with Pulse, Pediatric", Toast.LENGTH_SHORT).show();
					break;
				//pediatric tachycardia
				case 213:
					algorithmName = "V-Tach with Pulse, Pediatric";
					Toast.makeText(this, "V-Tach with Pulse, Pediatric", Toast.LENGTH_SHORT).show();
					break;
					
	/////---------------------------------
				default:
					algorithmName = "nothing";
					Toast.makeText(this, "Please Enter Data Again", Toast.LENGTH_SHORT).show();
					break;
				}
			}
			

		if (!algorithmName.equals("nothing")){
			 Intent nextActivity = new Intent();
			 nextActivity.setClassName("edu.c06190115.cardiac","edu.c06190115.cardiac.SetAlgorithm");
			 nextActivity.putExtra("edu.c06190115.cardiac.algorithm_name", algorithmName);
			 nextActivity.putExtra("edu.c06190115.cardiac.file_name", fileName);
             startActivity(nextActivity);
			}

		break;			

		}
			
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main__screen, menu);
		Spinner spinner1 = (Spinner)findViewById(R.id.heartRate);
		Spinner spinner2 = (Spinner)findViewById(R.id.heartRythm);
		Spinner spinner3 = (Spinner)findViewById(R.id.pedHeartRythm);
		Spinner spinner4 = (Spinner)findViewById(R.id.pedHeartRate);
		
		ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.heart_rate, R.layout.spinner_layout);
		adapter1.setDropDownViewResource(R.layout.spinner_dropdown_layout);
		ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.heart_rhythm, R.layout.spinner_layout);
		adapter2.setDropDownViewResource(R.layout.spinner_dropdown_layout);
		ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this, R.array.ped_heart_rhythm, R.layout.spinner_layout);
		adapter3.setDropDownViewResource(R.layout.spinner_dropdown_layout);
		ArrayAdapter adapter4 = ArrayAdapter.createFromResource(this, R.array.ped_heart_rate, R.layout.spinner_layout);
		adapter4.setDropDownViewResource(R.layout.spinner_dropdown_layout);
	
		spinner1.setAdapter(adapter1);
		spinner2.setAdapter(adapter2);
		spinner3.setAdapter(adapter3);
		spinner4.setAdapter(adapter4);
		
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.startEducation:
			Intent educationActivity = new Intent();
			educationActivity.setClassName("edu.c06190115.cardiac","edu.c06190115.cardiac.Education");
            educationActivity.putExtra("edu.c06190115.cardiac.file_name", fileName);
			startActivity(educationActivity);
			return true;
		}
		return false;
	}
}
